﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kugar.Core.BaseStruct;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RabbitMQ.Client;

namespace Kugar.Tool.RemoteEventBus.ClientTest
{
    class Program
    {


        static void Main(string[] args)
        {
            var connFactory = new ConnectionFactory()
            {
                HostName = "203.195.191.64",
                Port = 5672,
                VirtualHost = "developtest",
                UserName = "ylcoretest",
                Password = "123456",
                AutomaticRecoveryEnabled = true,
                RequestedHeartbeat = 10,
                NetworkRecoveryInterval = new TimeSpan(0, 0, 10)
            };

            var conn = connFactory.CreateConnection();
            conn.AutoClose = false;

            var bus=new RemoteEventBus();

            bus.Initialize(conn,"RemoteEvent_ClientTest_" + RandomEx.Next(100));

            var subscriber=bus.GetAsyncEventSubsciber<Test1EventArgs>("Test1");

            subscriber += Subscriber_EventRaising;

            //bus.InitCompleted();

            //Console.ReadLine();

            var sub2 = bus.GetSyncEventSubsecriber<Test2RaiseEventArgs>("Test2");

            sub2 += Test2EventRaise_Handle;


            //bus.AsyncEventRaised += Bus_AsyncEventRaised;
            //bus.SyncEventRaised += Bus_SyncEventRaised;
            //bus.SubscibeEvent(new string[] { "AsyncEventTest_1","SyncEventTest_2" });

            

            //Console.Read();
            //Console.ReadLine();



            Console.ReadLine();
        }

        private static void Subscriber_EventRaising(object sender, Test1EventArgs e)
        {
            Console.WriteLine($"Test1EventAsync={e.TestID}") ;
        }

        public class Test1EventArgs : EventArgs
        {
            public string TestID { set; get; }
        }

        private static void Bus_SyncEventRaised(object sender, Test1EventArgs e)
        {
            Console.WriteLine($"AsyncEventRaise_Test1={e.TestID}");

            //var args = JsonConvert.DeserializeObject<JObject>(e.Data);

            //e.ReturnValue=new JObject()
            //{
            //    ["B"]=(int)args.GetValue("A")
            //}.ToString(Formatting.None);
        }

        private static void Bus_AsyncEventRaised(object sender, AsyncEventRaisedEventArgs e)
        {
            //throw new NotImplementedException();
            Console.WriteLine($"AsyncEventRaise_{e.EventName}={e.Data}");
        }

        private static void Test2EventRaise_Handle(object sender,Test2RaiseEventArgs e)
        {
            Console.WriteLine($"触发同步事件={JsonConvert.SerializeObject(e)}");

            e.ReturnValue=new JObject()
            {
                ["RelyData"]="sdfsdfsdf",
                ["Tsdfsdf"]=e.Test2ID
            };


        }

        public class Test2RaiseEventArgs : SyncRaiseEventArgs
        {
            public string TestID { set; get; }

            public string Test2ID { set; get; }
        }
    }
}
